# -*- coding: utf-8 -*-
# @ Time   :  2020/8/26 15:33
# @ Author : Redtree
# @ File :  aif
# @ Desc :  ai相关算法


import random


#AI自动部署的算法
def create_ai_map():
    imap = []
    #先全部填充0
    for x in range(0, 15):
        heng = []
        for y in range(0, 15):
            heng.append(0)
        imap.append(heng)
    #写死AI部署顺序，加快部署速度 由大船到小船
    for bbn in range(1,6):
        cbn = 6-bbn
        #生成随机方向
        cdirect = random.choice([1,0])
        if cdirect==1:
            #竖直
            #当前能放置的起始坐标集
            ccs =[]
            for dy in range(0,15-cbn):
                for dx in range(0,15):
                    cc_flag=True
                    for ady in range(0,cbn):
                        if imap[dy+ady][dx]>0:
                            cc_flag=False
                            break
                    if cc_flag==True:
                        ccs.append((dy,dx))
            #随机从可部署位置中选择一个
            cpnine = random.choice(ccs)
            #更新到imap
            for ady in range(0, cbn):
                imap[cpnine[0]+ady][cpnine[1]]=cbn
        else :
            #横向
            #当前能放置的起始坐标集
            ccs =[]
            for dy in range(0,15):
                for dx in range(0,15-cbn):
                    cc_flag=True
                    for adx in range(0,cbn):
                        if imap[dy][dx+adx]>0:
                            cc_flag=False
                            break
                    if cc_flag==True:
                        ccs.append((dy,dx))
            #随机从可部署位置中选择一个
            cpnine = random.choice(ccs)
            #更新到imap
            for adx in range(0, cbn):
                imap[cpnine[0]][cpnine[1]+adx]=cbn
    return imap


#移动鼠标到射击点，自动避开射击过的地方，优先射击着火点周围
def aim_shoot_spxy(cbfm):
    #着火点
    firepis = []
    #海点
    seapis = []

    fix=0
    fiy=0
    for cm in cbfm:
        for c in cm:
            if c==-1:
                firepis.append([fix,fiy])
            elif c==0:
                seapis.append([fix,fiy])
            fix+=1
            if fix>14:
                fix=0
        fiy+=1

    #如果有着火点，优先攻击着火点周围
    if len(firepis)>0:
        cps = random.choice(firepis)
        #验证周围是否有海点
        zls = []
        for cx in [-1,0,1]:
            for cy in [-1,0,1]:
               if cx==0 and cy==0:
                   pass
               else:
                   if cbfm[cy][cx]==0:
                     if  0<=cps[0]+cx<15 and 0<=cps[1]+cy<15:
                        zls.append([cps[0]+cx,cps[1]+cy])
        #如果有,则攻击此点
        if len(zls)>0:
            zcps = random.choice(zls)
            return zcps
        else:
            #如果无，从海点里随机选择攻击
            zcps = random.choice(seapis)
            return zcps
    else:
        #没有着火点，随机攻击海点
        zcps = random.choice(seapis)
        return zcps



